package com.beijiteshop.shop.ui.notification.adapter

import androidx.databinding.ObservableBoolean
import androidx.databinding.ViewDataBinding
import com.beijiteshop.shop.R
import com.beijiteshop.shop.databinding.ItemNotificationDetailBinding
import com.beijiteshop.shop.databinding.ItemNotificationDetailDateBinding
import com.beijiteshop.shop.model.api.response.NotificationDetailEntity
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder

/**
 * @Author LiaoZhongKai
 * @Date 2021/3/16 11:42
 * @Description
 */
class NotificationDetailAdapter(data: MutableList<NotificationDetailEntity>)
    : BaseMultiItemQuickAdapter<NotificationDetailEntity,BaseDataBindingHolder<ViewDataBinding>>(data),LoadMoreModule {

    init {
        addItemType(NotificationDetailEntity.TYPE_DATE, R.layout.item_notification_detail_date)
        addItemType(NotificationDetailEntity.TYPE_ITEM, R.layout.item_notification_detail)
    }

    override fun convert(holder: BaseDataBindingHolder<ViewDataBinding>, item: NotificationDetailEntity) {
        holder.dataBinding?.let {binding ->
            when(item.itemType){
                NotificationDetailEntity.TYPE_DATE -> {
                    val dateBingDing = binding as? ItemNotificationDetailDateBinding
                    dateBingDing?.let {
                        it.date = item.date
                    }
                }
                NotificationDetailEntity.TYPE_ITEM -> {
                    val itemBingDing = binding as? ItemNotificationDetailBinding
                    itemBingDing?.let {
                        it.notification = item
                    }
                }
                else -> {}
            }
        }
    }

    /**
     * 更新消息已读状态
     */
    fun updateReadState(messageId: String){
        val item = data.find {
            it.message?.bus_MessageID == messageId
        }
        item?.isRead?.set(true)
    }

    /**
     * 更新所有消息为已读状态
     */
    fun updateAllReadState(){
        data.forEach {
            if (it.itemType == NotificationDetailEntity.TYPE_ITEM && !it.isRead.get()){
                it.isRead.set(true)
            }
        }
    }

}